import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class Client_Basic extends Frame implements WindowListener  {

Socket connection;

InputStream inStream;
DataInputStream inDataStream;

TextArea logDisplay;
String message;

  public Client_Basic ()  {

    super ( "Client_Basic " );
    logDisplay = new TextArea ( 40, 10 );
    add ( "Center", logDisplay );
    resize ( 400, 300 );
    show ();

  }  // end Client_Basic constructor

// **************

  public void runClient ( )  {

    String host;

    try  {

      try {
        InetAddress here = InetAddress.getLocalHost ();
        host = here.getHostName ();
        connection = new Socket ( host, 8901 );
        logDisplay.setText ( "Socket created:  connecting to server on "+host+"\n" );
        }
      catch (UnknownHostException e) {
        logDisplay.setText ( "Failed to create socket to server\n" );
        }

      inStream = connection.getInputStream ();
      inDataStream = new DataInputStream ( inStream );

      logDisplay.appendText ( "InputStream created\n" );
      logDisplay.appendText ( "Text received from server: \n     " );

      boolean more = true;
      try {
        while ( true ) {
            message = inDataStream.readUTF ();
            logDisplay.appendText ( message+"\n" );
        }  // end while
        }  // end try for input
      catch ( EOFException except ) {
            connection.close ();
            logDisplay.appendText ( "Connection closed\n" );
        }  // end catch
      catch ( IOException except ) {
        System.out.println ( "IO Exception");
        except.printStackTrace ();
        }  // end catch

      }  // end try for connection
    catch ( IOException except ) {
        System.out.println ( "Network Exception");
        except.printStackTrace ();
      }  // end catch

  }  // end runClient


// **************

  public static void main ( String [ ] args )  {

    Client_Basic client = new Client_Basic ();
    client.addWindowListener ( client );
    client.runClient ();

  }  // end main


  //***********  Utility Methods   ***********


  //***********  Interface Methods   ***********


  //****  WindowListener methods

  public void windowActivated ( WindowEvent e )  {
  }

  public void windowDeactivated ( WindowEvent e )  {
  }

  public void windowOpened ( WindowEvent e )  {
  }

   public void windowClosed ( WindowEvent e )  {
  }

  public void windowClosing ( WindowEvent e )  {
    this.hide ();
    this.dispose ();
    System.exit(0);
  }

  public void windowIconified ( WindowEvent e )  {
  }

  public void windowDeiconified ( WindowEvent e )  {
  }


}  // end Client_Basic